package com.gitee.wsl.markdown.plugin.base

import com.gitee.wsl.markdown.Markwon
import com.gitee.wsl.markdown.plugin.createMarkwonPlugin
import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.parser.markerblocks.impl.AtxHeaderMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.BlockQuoteMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.CodeBlockMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.CodeFenceMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.HorizontalRuleMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.HtmlBlockMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.LinkReferenceDefinitionMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.ListMarkerBlock
import org.intellij.markdown.parser.markerblocks.impl.SetextHeaderMarkerBlock
import org.intellij.markdown.parser.markerblocks.providers.AtxHeaderProvider
import org.intellij.markdown.parser.markerblocks.providers.BlockQuoteProvider
import org.intellij.markdown.parser.markerblocks.providers.CodeBlockProvider
import org.intellij.markdown.parser.markerblocks.providers.CodeFenceProvider
import org.intellij.markdown.parser.markerblocks.providers.HorizontalRuleProvider
import org.intellij.markdown.parser.markerblocks.providers.HtmlBlockProvider
import org.intellij.markdown.parser.markerblocks.providers.LinkReferenceDefinitionProvider
import org.intellij.markdown.parser.markerblocks.providers.ListMarkerProvider
import org.intellij.markdown.parser.markerblocks.providers.SetextHeaderProvider
import org.intellij.markdown.parser.sequentialparsers.EmphasisLikeParser
import org.intellij.markdown.parser.sequentialparsers.impl.AutolinkParser
import org.intellij.markdown.parser.sequentialparsers.impl.BacktickParser
import org.intellij.markdown.parser.sequentialparsers.impl.EmphStrongDelimiterParser
import org.intellij.markdown.parser.sequentialparsers.impl.ImageParser
import org.intellij.markdown.parser.sequentialparsers.impl.InlineLinkParser
import org.intellij.markdown.parser.sequentialparsers.impl.ReferenceLinkParser

/*class CorePlugin(val ableHtml:Boolean = true) : AbstractMarkwonPlugin() {
    override val key: AttributeKey<MarkwonPlugin> = AttributeKey("Core")

    override fun prepare(block: MarkwonConfiguration.() -> Unit): MarkwonPlugin {
        return this
    }

    override fun configureParser(builder: MutableMarkdownFlavourDescriptor) {
        super.configureParser(builder)
        builder.registerMarkerBlockProvider(
            CodeBlockMarkerBlock::class to CodeBlockProvider(),
            HorizontalRuleMarkerBlock::class to HorizontalRuleProvider(),
            CodeFenceMarkerBlock::class to CodeFenceProvider(),
            SetextHeaderMarkerBlock::class to SetextHeaderProvider(),
            BlockQuoteMarkerBlock::class to BlockQuoteProvider(),
            ListMarkerBlock::class to ListMarkerProvider(),
            AtxHeaderMarkerBlock::class to AtxHeaderProvider(),
            HtmlBlockMarkerBlock::class to HtmlBlockProvider(),
            LinkReferenceDefinitionMarkerBlock::class to LinkReferenceDefinitionProvider()
        )

        builder.registerSequentialParser(
            AutolinkParser(listOf(MarkdownTokenTypes.AUTOLINK)),
            BacktickParser(),
            ImageParser(),
            InlineLinkParser(),
            ReferenceLinkParser(),
            EmphasisLikeParser(EmphStrongDelimiterParser())
        )

    }

    override fun install(plugin: MarkwonPlugin, scope: Markwon) {

    }

}*/

val Markwon.Companion.PLUGIN_CORE get() = "core"

fun Markwon.useCore():Markwon{
    val corePlugin = createMarkwonPlugin(Markwon.PLUGIN_CORE){
        configureParser {

            registerMarkerBlockProvider(
                CodeBlockMarkerBlock::class to CodeBlockProvider(),
                HorizontalRuleMarkerBlock::class to HorizontalRuleProvider(),
                CodeFenceMarkerBlock::class to CodeFenceProvider(),
                SetextHeaderMarkerBlock::class to SetextHeaderProvider(),
                BlockQuoteMarkerBlock::class to BlockQuoteProvider(),
                ListMarkerBlock::class to ListMarkerProvider(),
                AtxHeaderMarkerBlock::class to AtxHeaderProvider(),
                HtmlBlockMarkerBlock::class to HtmlBlockProvider(),
                LinkReferenceDefinitionMarkerBlock::class to LinkReferenceDefinitionProvider()
            )

            registerSequentialParser(
                AutolinkParser(listOf(MarkdownTokenTypes.AUTOLINK)),
                BacktickParser(),
                ImageParser(),
                InlineLinkParser(),
                ReferenceLinkParser(),
                EmphasisLikeParser(EmphStrongDelimiterParser())
            )
        }

    }
    config.install(corePlugin)
    return this
}

